<template>
  <div class="header">
    <div class="page-title">酒店管理</div>
    <el-button
        class="m-t-15"
        type="primary"
        size="small"
        icon="el-icon-edit"
        @click="handleCCreate"
    >酒店列表</el-button
    >
    <el-form>
      <el-form-item label="酒店名称">
        <el-input
            v-model="form.hotel_name"
            autocomplete="off"
            placeholder="请输入名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="酒店类型">
        <el-select v-model="form.hotel_type" placeholder="请选择" style="margin-left:10px">
          <el-option
              v-for="item in hoteltype"
              :key="item.id"
              :label="item.name"
              :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="酒店地址">
        <el-input
            v-model="form.hotel_address"
            autocomplete="off"
            placeholder="请输入酒店地址"
        ></el-input>
      </el-form-item>
      <el-form-item label="区域">
        <el-select v-model="form.hotel_area" placeholder="请选择" style="margin-left:10px">
          <el-option
              v-for="item in packages"
              :key="item.id"
              :label="item.content"
              :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="酒店电话">
        <el-input
            v-model="form.hotel_phone"
            autocomplete="off"
            placeholder="请输入酒店电话"
        ></el-input>
      </el-form-item>

      <el-form-item label="是否置顶">
        <el-radio v-model="form.is_top" label="0">否</el-radio>
        <el-radio v-model="form.is_top" label="1">是</el-radio>
      </el-form-item>

      <el-form-item label="是否热门">
        <el-radio v-model="form.is_hot" label="0">否</el-radio>
        <el-radio v-model="form.is_hot" label="1">是</el-radio>
      </el-form-item>
      <el-form-item label="价格">
        <el-input
            v-model="form.sort"
            autocomplete="off"
            placeholder="请输入价格"
        ></el-input>
<!--        <el-select v-model="form.budget" placeholder="请选择" style="margin-left:10px">
          <el-option
              v-for="items  in price"
              :key="items.id"
              :label="items.name"
              :value="items.id">
          </el-option>
        </el-select>-->
      </el-form-item>
      <el-form-item label="酒店图片">
        <el-upload
            class="upload-demo"
            :action="uploadUrl"
            :on-preview="handlePreview"
            :on-success="handleUploadSuccess"
            :before-upload="beforeAvatarUpload"
            :before-remove="beforeRemove"
            :on-remove = "handleRemoveAvatar"
            :multiple = true
            :file-list="form.hotel_image_url"
            list-type="picture"
        > <el-button size="small" type="primary">点击上传</el-button>
        </el-upload>
      </el-form-item>
      <el-form-item label="酒店视频">
        <el-upload
            class="upload-demo"
            :action="uploadUrl"
            :on-success="handleUploadVideoSuccess"
            :before-upload="beforeAvatarVideoUpload"
            :before-remove="beforeRemove"
            :on-remove = "handleRemoveAvatarVideo"
            :multiple = true
            :file-list="form.hotel_video_url"
            list-type="video"
        >
<!--          <img v-if="form.hotel_video_url" :src="form.hotel_video_url"  style="width:100px" />-->
<!--          <a v-if="form.hotel_video_url" href="javascript:void(0);">视频</a>-->
          <el-button size="small" type="primary">点击上传</el-button>
          <div  v-for="(item,key) in form.hotel_video_url" :key="key" style="height:185px;width:205px">
            <video v-if="item.url !='' && !videoFlag"
                   v-bind:src="item.url"
                   class="avatar video-avatar"
                   controls="controls" style="height:180px;width:200px">
              您的浏览器不支持视频播放
            </video>
          </div>
        </el-upload>
      </el-form-item>
      <el-form-item label="桌数">
        <el-input
            v-model="form.zhuo_num"
            autocomplete="off"
            placeholder="请输入桌数"
        ></el-input>
      </el-form-item>
      <el-form-item label="评分">
        <el-input
            v-model="form.score"
            autocomplete="off"
            placeholder="请输入评分"
        ></el-input>
      </el-form-item>
      <el-form-item label="排序">
        <el-input
            v-model="form.sort"
            autocomplete="off"
            placeholder="请输入评分"
        ></el-input>
      </el-form-item>
      <el-form-item label="酒店介绍">
        <Editor :value="form.hotel_content" @change="changeEditor" style="width:75%"></Editor>
      </el-form-item>
    </el-form>


      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSubmitForm">确 定</el-button>
      </div>
  </div>


</template>
<script>
import Editor from '@/components/Editor'
import { UPLOAD_URL,CONST_DMOMAIN_URL } from "@/config/env";
import {
  request_hotel_detail,
  request_area_list,
  request_hotel_create,
  request_hotel_update,
  request_hotel_delete,
  request_price_list, request_hotelType_list,
} from "@/util/api";
export default {
  name: "Hotel_add",
  data() {
    return {
      current: 1,
      total: 0,
      form: {
        is_top: "0",
        is_hot: "0",
        hotel_content:'',
        hotel_video_url:[],
        hotel_image_url:[]
      },
      // 门店是否显示
      dialogFormVisible: false,
      uploadUrl: UPLOAD_URL,
      actionType: 0, // 1 添加  2 编辑
      packages:[],
      price:[],
      hoteltype:[]
    };
  },
  components: {
    Editor
  },
  //computed: {},
  mounted() {
    this.init();
    let id = this.$route.query.id;
    if(id){
      this.actionType=2
      request_hotel_detail( {
        id:id
      }).then(res => {
        const result = res.data
        if (result.code == 200) {
          this.form = result.data
          this.form.is_top = this.form.is_top.toString()
          this.form.is_hot = this.form.is_hot.toString()
        }
      }).catch()
    }else{
      this.actionType=1
    }
  },
  methods: {
    init() {
      this.$nextTick(() => {
        request_area_list()
            .then((res) => {
              const result = res.data;
              if (result.code == 200) {
                this.packages = result.data;
              }
            });
        request_price_list()
            .then((res) => {
              const result = res.data;
              if (result.code == 200) {
                this.price = result.data;
              }
            });
        request_hotelType_list()
            .then((res) => {
              const result = res.data;
              if (result.code == 200) {
                this.hoteltype = result.data;
              }
            });
      });
    },
    // 富文本内容
    changeEditor(val) {
      this.form.hotel_content = val
    },
    removeBanner(key){
      this.$confirm("确定要删除此图片吗？", "系统提示", {
        type: "warning",
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
          .then(() => {
            this.form.banners.splice(key, 1)
          })

    },
    handleStation(row) {
      this.$router.push('/hall?id=' + row.id)
    },
    //发布视频上传成功的回调函数
    handleUploadVideoSuccess(res) {
      this.form.hotel_video_url.push({'url':CONST_DMOMAIN_URL +res.data});
    },
    //发布图片上传成功的回调函数
    handleUploadSuccess(res) {
      this.form.hotel_image_url.push({'url':CONST_DMOMAIN_URL +res.data});
    },

    handleUploadSuccessByBanners(res,raw, key) {
      this.form.banners[key] = CONST_DMOMAIN_URL + res.data;
      console.log(this.form.banners)
    },
    //上传视频前的验证函数
    beforeAvatarVideoUpload(file) {
      let types = ['video/mp4'];
      const isJPG=types.includes(file.type)
      // const isJPG = file.type === "image/png";验证单张图片
      const isLt2M = file.size / 1024 / 1024 < 5;
      if (!isJPG) {
        this.$message.error("上传视频只能是 mp4 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传视频大小不能超过 5MB!");
      }
      return isJPG && isLt2M;
    },
    //图片上传前的验证函数
    beforeAvatarUpload(file) {
      let types = ['image/jpeg', 'image/jpg', 'image/gif', 'image/bmp', 'image/png'];
      const isJPG=types.includes(file.type)
      // const isJPG = file.type === "image/png";验证单张图片
      const isLt2M = file.size / 1024 / 1024 < 3;
      if (!isJPG) {
        this.$message.error("上传图片只能是 jpg,jpeg,bmp,png 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 3MB!");
      }
      return isJPG && isLt2M;
    },
    handleRemoveAvatar(file,fileList){
      this.form.hotel_image_url = fileList
    },
    handleRemoveAvatarVideo(file,fileList){
      this.form.hotel_video_url = fileList
      console.log(this.form.hotel_video_url)
    },
    // 添加
    handleCCreate() {
      /* this.actionType = 1;
       this.form = {banners:[''], is_top: "0"};
       this.dialogFormVisible = true;*/
      this.$router.push('/hotel')
    },
    // 编辑
    handleEdit(row) {
      this.actionType = 2;
      this.form = row;
      this.form.is_top = this.form.is_top.toString()
      this.form.is_hot = this.form.is_hot.toString()
      this.dialogFormVisible = true;
    },
    // 提交
    handleSubmitForm() {
      let apiFunc;
      switch (this.actionType) {
        case 1: // 添加
          apiFunc = request_hotel_create;
          break;
        case 2: // 编辑
          apiFunc = request_hotel_update;
          break;
        default:
          this.$message.error({
            message: "操作类型不正确",
            offset: 70,
          });
          return false;
      }
      console.log("提交的数据",this.form);
      apiFunc({
        ...this.form,
      }).then((res) => {
        const result = res.data;
        if (result.code == 200) {
          this.dialogFormVisible = false;
          this.$notify.success({
            title: "系统提示",
            message: result.message,
            offset: 70,
          });
          this.$router.push('/hotel')
        } else {
          this.$message.error({
            message: result.message,
            offset: 70,
          });
        }
      });
    },
    // 删除
    handleDelete(row) {
      this.$confirm("此操作将永久删除，是否继续？", "系统提示", {
        type: "warning",
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
          .then(() => {
            request_hotel_delete({
              id: row.id,
            })
                .then((res) => {
                  const result = res.data;
                  if (result.code == 200) {
                    this.$message.success({
                      message: "删除成功！",
                      offset: 70,
                    });
                    this.changePage(this.page)
                  } else {
                    this.$message.error({
                      message: result.message,
                      offset: 70,
                    });
                  }
                })
                .catch();
          })
          .catch(() => {
            this.$message.info({
              message: "已取消删除！",
              offset: 70,
            });
          });
    },

  },
};
</script>

<style scoped  lang="less">
.avatar-uploader {
  float:left;
  margin-right:10px;
  margin-bottom:10px;
}
.banner-add {
  clear:both;
  margin-top:20px;
}
</style>